package tgc.edu.ljc.web.controller;

import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.ljc.custom.CommonController;
import tgc.edu.ljc.entity.Permission;
import tgc.edu.ljc.web.form.PermissionForm;



/***
 * 权限控制
 * @author zmq
 *
 */

@Controller
@RequestMapping(value="/permission")
public class PermissionController extends CommonController<Permission, Integer, PermissionForm> {
	
	@Override
	public Specification<Permission> buildSpec(PermissionForm form) {
		Specification<Permission> specification = new Specification<Permission>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<Permission> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(StringUtils.hasText(form.getName())) {
					Predicate name = cb.like(root.get("name"), "%"+form.getName()+"%");
					rules.add(name);
				}
				if(StringUtils.hasText(form.getCode())) {
					Predicate difficultylevel = cb.like(root.get("code"), "%"+form.getCode()+"%");
					rules.add(difficultylevel);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}
}
